home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 178_01 / tvx_io.c < prev    next >
C/C++ Source or Header  |  1986-01-16  |  42KB  |  1,870 lines

  1. /* ---------------------------- tvx_io.c ------------------------------- */
  2. #include "tvx_defs.ic"
  3. #include "tvx_glbl.ic"
  4.  
  5. #define SWITCH '-'
  6. #define FILESEP '.'
  7.  
  8. #ifdef MSDOS
  9. #define TEMPEXT ".$$1"        /* name of temporary file */
  10. #define BACKEXT ".BAK"        /* name of backup file */
  11. #endif
  12.  
  13. #ifdef OSCPM
  14. #define TEMPEXT ".$$1"        /* name of temporary file */
  15. #define BACKEXT ".BAK"        /* name of backup file */
  16. #endif
  17.  
  18. #ifdef GEMDOS
  19. #define TEMPEXT ".Z1X"        /* name of temporary file */
  20. #define BACKEXT ".BAK"        /* name of backup file */
  21. #endif
  22.  
  23. #ifdef UNIX
  24. #define BACKEXT ".B"        /* name of backup file */
  25. #endif
  26.  
  27.     FILE *fopen();
  28.  
  29. /* local globals (used by tv terminal driver section) */
  30.  
  31.     static int linptr; /* common "linot" */
  32.     static char linout[242];
  33.  
  34.     static char stemp[FNAMESIZE+1];
  35.  
  36. /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  37.  
  38.     FILE IO section
  39.  
  40.    File handling algorithm:
  41.  
  42.     The original name specified on command line is called orig_file.
  43.     It will remain untouched throughout the editing session.  At the
  44.     very end (normal exit), it will be renamed to the ".BAK" name.
  45.  
  46.     source_file is the current name of the file with source.  It will
  47.     orignally be the same as orig_file, but may change to a generated
  48.     scratch name depending on the operating system.  source_file is
  49.     always the lastest fully written version of the file (like after
  50.     file beginning, for example). 
  51.  
  52.     work_file is the output file.  On normal exit, this is the
  53.     file renamed to dest_file.  On buffer beginning, it will be
  54.     moved to source_file, possibly after renameing.
  55.     
  56.     dest_file is the ultimate destination file.  This name is not
  57.     actually used until the final rename on normal exit.  It is
  58.     checked to be sure it is a valid name to be opened, however.
  59.  
  60.    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
  61.  
  62. /* =============================>>> ABORT <<<============================= */
  63.   abort()
  64.   {    /* abort - close files, abort operation */
  65.  
  66.     char rply[4];
  67.  
  68.     tvclr();
  69.     ask("Abort, are you sure? ",rply,1);
  70.     if (clower(rply[0]) != 'y')
  71.       {
  72.     verify(1);
  73.     return;
  74.       }
  75.     abort2();
  76.   }
  77.  
  78. /* =============================>>> ABORT2 <<<============================= */
  79.   abort2()
  80.   {
  81.     clobak();
  82.     tvclr();
  83.  
  84.     if (!newfil)
  85.     fclose(infile);
  86.     if (!rdonly)
  87.     fclose(outfile);
  88.  
  89.     if (strcmp(orig_file,source_file) != 0)
  90.       {
  91.     prompt("File begin used, intermediate edits in: ");
  92.     remark(source_file);
  93.       }
  94.     unlink(work_file);        /* delete the work file */
  95.  
  96.     reset();
  97.     quit();
  98.   }
  99.  
  100. /* =============================>>> FBEG   <<<============================= */
  101.   int fbeg()
  102.   { /* fbeg - go back to file top */
  103.  
  104.     SLOW int fbegv;
  105.  
  106.     if (rdonly)
  107.       {
  108.     tverrb("Can't: R/O");    /* can't do this for read only access */
  109.     return (FALSE);
  110.       }
  111.  
  112.     for (wtpage(1) ; rdpage() ; wtpage(1) )    /* write out rest */
  113.     ;
  114.  
  115.     if ((! newfil))
  116.       {
  117.     fclose(infile);            /* close source_file */
  118.       }
  119.     if (usecz)
  120.     fputc(ENDFILE,outfile);
  121.  
  122.     fclose(outfile);            /* close work_file */
  123.  
  124. /* files closed now, re-open */ 
  125.  
  126.     newfil = FALSE;        /* not a new file any more */
  127.  
  128.     strcpy(source_file,work_file);    /* new source file */
  129.     temp_name(work_file,FALSE);        /* make a new temporary name */
  130.  
  131.     if (!(infile = fopen(source_file,FILEREAD)))
  132.     goto l900;
  133.     else
  134.     ineof = FALSE;
  135.  
  136.     unlink(work_file);            /* get rid of previous copies */
  137.     if (!(outfile = fopen(work_file,FILEWRITE)))
  138.       {
  139.     goto l900;
  140.       }
  141.  
  142.     fbegv=rdpage();        /* read in new buffer */
  143.     newscr();
  144.     return (fbegv);
  145.  
  146. l900: tverrb("Error re-opening");
  147.     return (FALSE);
  148.   }
  149.  
  150. /* =============================>>> FILE_EXIT <<<============================= */
  151.   file_exit()
  152.   { /* close the input and output files, rename */
  153.  
  154.     SLOW int i;
  155.  
  156.     if (!newfil)        /* don't close input if new file */
  157.       {
  158.     fclose(infile);
  159.       }
  160.  
  161.     while (!rdonly && !*dest_file)
  162.       {
  163.     remark("No name for output file has been specified.");
  164.         prompt("Enter new name for output file: ");
  165.     reply(dest_file,FNAMESIZE);
  166.       }
  167.  
  168.     if (!rdonly)    /* don't close output if read only access */
  169.       {
  170.     if (usecz)
  171.         fputc(ENDFILE,outfile);
  172.     set_mode(outfile);        /* set output mode if can */
  173.     fclose(outfile);
  174.  
  175.     /*    orig_file has the name to be renamed to .bak
  176.     work_file has the file name we want to be dest_name
  177.     */
  178.     if (strcmp(orig_file,dest_file) == 0)    /* make backup version */
  179.       {
  180.         strcpy(stemp,orig_file);
  181. #ifndef COMMA_BAK
  182.         if ((i = rindex(stemp,FILESEP)) > 0)    /* see if extenstion */
  183.         scopy(BACKEXT,0,stemp,i);        /* make .bak */
  184.         else
  185.           {
  186.         scopy(BACKEXT,0,stemp,strlen(stemp));    /* just add on */
  187.           }
  188. #else
  189.         i = rindex(orig_file,'/')+1;
  190.         scopy(".,",0,stemp,i);
  191.         scopy(orig_file,i,stemp,strlen(stemp));
  192. #endif
  193.  
  194.         unlink(stemp);            /* delete previous generation */
  195.         ren_file(orig_file,stemp);        /* rename the file */
  196.         if (!makebackup)            /* don't want to keep old one */
  197.         unlink(stemp);    /* delete it if don't want backup file */
  198.       }
  199.  
  200.     if (strcmp(orig_file,source_file) != 0)    /* delete intermediate file */
  201.         unlink(source_file);
  202.  
  203.  
  204.     while (infile = fopen(dest_file,FILEREAD))    /* output exists? */
  205.       {
  206.         fclose(infile);
  207.         prompt("Output file "); prompt(dest_file);
  208.         prompt(" already exists.  Overwrite it? (y/n) ");
  209.         ureply(stemp,1);
  210.         if (*stemp == 'Y')
  211.           {
  212.         unlink(dest_file);
  213.         break;
  214.           }
  215.         prompt("Enter new name for output file: ");
  216.         reply(dest_file,FNAMESIZE);
  217.       }
  218.  
  219.     ren_file(work_file,dest_file);        /* finally, rename last file */
  220.       }
  221.  
  222.   }
  223.  
  224. /* =============================>>> FOPENX  <<<============================= */
  225.   fopenx(argc,argv)
  226.   int argc;
  227.   char *argv[];
  228.   {  /* open the input file
  229.     This routine picks up file name from the user, creates a backup
  230.     version in some appropriate manner, and opens the file for input
  231.     and output. */
  232.  
  233.     SLOW int iswval, iswbeg, argnum, set_ttymode;
  234.     SLOW char ch;
  235.     char rply[4];
  236.  
  237.     usebak = logdef;        /* if useing backup log file */
  238.  
  239.     ttymode = FALSE;        /* not in tty mode, so io ok */
  240.     ttynext = 1000;        /* force read */
  241.  
  242.     if (argc <= 1)
  243.       {
  244.     remark("Usage: tvx filename [-b -i -l -o=f -r -s -t -w -# {-z -c=f}]");
  245. #ifdef FULLHELP
  246.     remark("");
  247.     prompt(" Options: "); remark(VERSION);
  248.     remark("  -[no]b : backup file   -[no]i : autoindent");
  249.     remark("  -[no]l : make command log file");
  250.     remark("  -o=outputfile          -r : read only");
  251.     remark("  -s : big save buff     -[no]w : word processing mode");
  252.     remark("  -t : tty edit mode     -# : set virtual window lines to #");
  253. #ifdef MSDOS
  254.         remark("  -[no]z : use control-z for end of file");
  255. #endif
  256. #ifdef CONFIGFILE
  257. #ifdef MSDOS
  258.         remark("  -c=configfile        -c : use /bin/config.tvx");
  259. #endif
  260. #ifdef GEMDOS
  261.         remark("  -c=configfile        -c : use /bin/config.tvx");
  262. #endif
  263. #ifdef OSCPM
  264.         remark("  -c=configfile        -c : use A:config.tvx");
  265. #endif
  266. #endif
  267. #ifdef UNIX
  268.     remark("  {options not available for unix}");
  269. #endif
  270. #endif
  271.     remark("");
  272.     reset();
  273.     quit();
  274.       }
  275.  
  276.     newfil=                /* assume opening an old file */
  277.     rdonly = FALSE;            /* assume not read only */
  278.     makebackup = MAKE_BACKUP;        /* default value for make a backup */
  279.     blimit = BUFFLIMIT;
  280.  
  281.     for (argnum = 1 ; argnum < argc ; ++argnum)
  282.       {
  283.     strcpy(stemp,argv[argnum]);    /* pick up the file name or switch */
  284. REDO:
  285.     if (stemp[0] == SWITCH)        /* switch in form "/R filename" only */
  286.       {
  287.         iswbeg=1;        /* start at 1 */
  288.         iswval = TRUE;
  289.         if (clower(stemp[1]) == 'n' && clower(stemp[2]) == 'o')
  290.           {
  291.         iswval = FALSE ; iswbeg = 3 ;
  292.           }
  293.  
  294.         ch = clower(stemp[iswbeg]);        /* get the char */
  295.         if (ch == 'r')        /* read only access */
  296.         rdonly=iswval;
  297.         else if (ch == 'i')        /* auto indent */
  298.         autoin = iswval;
  299.         else if (ch == 'w')        /* word processing mode */
  300.           {
  301.         if (iswval)
  302.             w